package com.jzo2o.foundations.service.impl;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzo2o.common.expcetions.DBException;
import com.jzo2o.common.expcetions.ForbiddenOperationException;
import com.jzo2o.common.model.PageResult;
import com.jzo2o.foundations.enums.FoundationStatusEnum;
import com.jzo2o.foundations.mapper.ServeMapper;
import com.jzo2o.foundations.model.domain.Serve;
import com.jzo2o.foundations.model.domain.ServeItem;
import com.jzo2o.foundations.model.domain.ServeType;
import com.jzo2o.foundations.model.dto.request.ServePageQueryReqDTO;
import com.jzo2o.foundations.model.dto.request.ServeUpsertReqDTO;
import com.jzo2o.foundations.model.dto.response.ServeResDTO;
import com.jzo2o.foundations.model.dto.response.ServeTypeResDTO;
import com.jzo2o.foundations.service.IRegionService;
import com.jzo2o.foundations.service.IServeService;
import com.jzo2o.foundations.service.IServeTypeService;
import com.jzo2o.mysql.utils.PageHelperUtils;
import com.jzo2o.mysql.utils.PageUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;

@Service
public class IServeServiceImpl extends ServiceImpl<ServeMapper, Serve> implements IServeService {
    @Autowired
    private ServeItemServiceImpl itemService;
    @Autowired
    private IRegionService regionService;
    @Autowired
    private IServeTypeService serveTypeService;

    /**
     * 分页查询
     *
     * @param servePageQueryReqDTO 查询条件
     * @return
     */
    @Override
    public PageResult<ServeResDTO> page(ServePageQueryReqDTO servePageQueryReqDTO) {
        PageResult<ServeResDTO> serveResDTOPageResult = PageHelperUtils.selectPage(servePageQueryReqDTO, () -> baseMapper.queryServeListByRegionId(servePageQueryReqDTO.getRegionId()));
        return serveResDTOPageResult;
    }

    /**
     * 添加区域服务
     *
     * @param serveUpsertReqDTOList
     */
    @Override
    public void add(List<ServeUpsertReqDTO> serveUpsertReqDTOList) {
        serveUpsertReqDTOList.forEach(
                dto -> {
                    //服务项要存在
                    ServeItem one = itemService.lambdaQuery().eq(ServeItem::getId, dto.getServeItemId()).one();
                    if (one == null) {
                        throw new ForbiddenOperationException("服务项不存在不能添加");
                    }

                    //服务项要启用
                    if (one.getActiveStatus() != 2) {
                        throw new ForbiddenOperationException("服务项没有启动");
                    }

                    //区域服务不能重复添加
                    Serve one1 = this.lambdaQuery().eq(Serve::getServeItemId, dto.getServeItemId())
                            .eq(Serve::getRegionId, dto.getRegionId())
                            .one();
                    if (one1 != null) {
                        throw new ForbiddenOperationException("区域服务不能重复添加");
                    }
                    String cityCode = regionService.getById(dto.getRegionId()).getCityCode();
                    //封装返回
                    Serve serve = new Serve();
                    BeanUtils.copyProperties(dto, serve);
                    serve.setCityCode(cityCode);
                    boolean result = this.save(serve);
                    if (!result) {
                        throw new ForbiddenOperationException("添加服务项失败");
                    }

                }
        );
    }

    /**
     * 修改商品价格
     *
     * @param id
     * @param price
     */
    @Override
    public void updatePrice(Long id, BigDecimal price) {
        Serve one = this.lambdaQuery().eq(Serve::getId, id).one();
        if (one == null) {
            throw new ForbiddenOperationException("不存在该区域服务");
        }
        boolean update = this.lambdaUpdate().eq(Serve::getId, id)
                .set(Serve::getPrice, price)
                .update();
        if (!update) {
            throw new ForbiddenOperationException("更新价格失败");
        }
    }

    /**
     * 区域服务上架
     *
     * @param id
     */
    @Override
    @Transactional
    public void updateStatus(Long id) {
        //1.判断服务是否存在
        Serve serve = this.getById(id);
        if (ObjectUtils.isEmpty(serve)) {
            throw new ForbiddenOperationException("服务不存在，禁止上架");
        }

        //2.判断是否有区域
        if (serve.getRegionId() == null) {
            throw new ForbiddenOperationException("区域未选，禁止上架");
        }

        //3.判断服务是否上架过
        if (serve.getSaleStatus() == FoundationStatusEnum.ENABLE.getStatus()) {
            throw new ForbiddenOperationException("不要重复上架");
        }

        //4.判断服务项是否存在
        ServeItem item = itemService.getById(serve.getServeItemId());
        if (ObjectUtils.isEmpty(item)) {
            throw new ForbiddenOperationException("服务项不存在，禁止上架");
        }

        //5.判断服务项是否已启动
        if (item.getActiveStatus() != FoundationStatusEnum.ENABLE.getStatus()) {
            throw new ForbiddenOperationException("服务项未启用，禁止上架");
        }

        //5.修改状态为已上架
        boolean result = this.lambdaUpdate().set(Serve::getSaleStatus, FoundationStatusEnum.ENABLE.getStatus()).eq(Serve::getId, id).update();
        if (!result) {
            throw new DBException("服务上架失败");
        }
    }

    @Override
    public void deleteServe(Long id) {
        boolean b = this.removeById(id);
        if(!b){
            throw new ForbiddenOperationException("删除服务失败");
        }
    }

    @Override
    public void offSale(Long id) {
        Serve one = this.lambdaQuery().eq(Serve::getId, id).one();
        if(one==null){
            throw new ForbiddenOperationException("该服务不存在");
        }
        if(one.getSaleStatus()==FoundationStatusEnum.DISABLE.getStatus() || one.getSaleStatus()==FoundationStatusEnum.INIT.getStatus()){
            throw new ForbiddenOperationException("该服务不能下架");
        }
        boolean update = this.lambdaUpdate().eq(Serve::getId, id).set(Serve::getSaleStatus, FoundationStatusEnum.DISABLE).update();
        if(!update){
            throw new ForbiddenOperationException("下架失败");
        }
    }
}
